// #include "../include/redis.h"
#include "fileSystem.h"

FileSystem::FileSystem() {
    initCommandTypes();
    IOControlLayer::GetInstance();
}
FileSystem::~FileSystem() { }

std::string FileSystem::command(const std::string& command) {
    std::istringstream iss(command);
    std::string type, key;
    iss >> type;
    if (type.empty()) {
        std::cout << "Wrong command" << std::endl;
        return " ";
    }

    BasicsCommand* basicsCommand = plantDataModel(type);
    if (basicsCommand == nullptr) {
        std::cout << "No data model found for type: " << type << std::endl;
        return "";
    }

    std::string result = basicsCommand->executeCommand(iss);
    if (!result.empty()) std::cout << result << std::endl;
    return "111";
}

void FileSystem::setPwd(const std::string &path) {
    pwd = path;
}

std::string FileSystem::getPwd() {
    return pwd;
}

BasicsCommand *FileSystem::plantDataModel(std::string_view type) {
    if (commandTypes.count(type) == 0) {
        return nullptr;
    }
    CommandType redisDataType = commandTypes[type];
    switch (redisDataType)
    {
    case CommandType::LS:
        return &Ls::GetInstance();
    case CommandType::MKDIR:
        return &Mkdir::GetInstance();
    case CommandType::CD:
        return &Cd::GetInstance();
    case CommandType::TOUCH:
        return &Touch::GetInstance();
    case CommandType::CAT:
        return &Cat::GetInstance();
    default:
        std::cout << "no data model found for type: " << type << std::endl;
        return nullptr;
    }
    return nullptr;
}

void FileSystem::initCommandTypes() {
    commandTypes["ls"] = CommandType::LS;
    commandTypes["mkdir"] = CommandType::MKDIR;
    commandTypes["cd"] = CommandType::CD;
    commandTypes["touch"] = CommandType::TOUCH;
    commandTypes["cat"] = CommandType::CAT;
    // commandTypes["rm"] = CommandType::RM;
    // commandTypes["cp"] = CommandType::CP;
    // commandTypes["mv"] = CommandType::MV;
}

